package com.example.administrator.szb.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.RzBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.fragment_forMy.FragmentWDGL;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.Toasts;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by D N on 2017/11/20.
 */

public class WDGLNEWActivity extends BaseActivity {
    @Bind(R.id.wdgl_wtcdwx)
    RadioButton wdglWtcdwx;
    @Bind(R.id.wdgl_wsddwt)
    RadioButton wdglWsddwt;
    @Bind(R.id.wdgl_whddwt)
    RadioButton wdglWhddwt;
    @Bind(R.id.my_answer_radiogroup)
    RadioGroup myAnswerRadiogroup;
    @Bind(R.id.wdgl_framLayout_content)
    FrameLayout wdglFramLayoutContent;
    @Bind(R.id.wdgl_qx)
    TextView wdglQx;
    @Bind(R.id.wdgl_sc)
    TextView wdglSc;
    @Bind(R.id.wdgl_ll)
    LinearLayout wdglLl;
    String bj_or_qx = "编辑";

    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragmentArrayList;
    private FragmentWDGL fragment_wtcdwt,fragment_wsddwt,fragment_whddwt;
    private FragmentTransaction fragmentTransaction;
    private Bundle bundle = new Bundle();


    int type = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wdgl_gw_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);

        if (type == 1){
            initToolbar_wdgl();
        }



        requestWDGL();
    }

    private void initToolbar_wdgl() {
        initToolbarThree(R.id.wdgl_toolbar_include, "问答管理", "" + bj_or_qx, new Callback() {
            @Override
            public void doSomeThing() {
                String s = ((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).getText().toString()+type;
                QTLog.e(s);
                if (((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).getText().equals("编辑")) {

                    if (type == 1||type == 3||type == 2){
                        ((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).setText("取消");
                        wdglLl.setVisibility(View.VISIBLE);
                        showCheck();
                    }else{
                        ((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).setText("编辑");
                        wdglLl.setVisibility(View.GONE);
                    }







                } else {
                    ((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).setText("编辑");
                    wdglLl.setVisibility(View.GONE);

                    hideCheck();
                }


            }
        });

    }

    private void hideCheck() {
        switch (type){
            case 1:
                for (int i = 0; i < fragment_wtcdwt.mdata.size(); i++) {
                    fragment_wtcdwt.mdata.get(i).setIsShowCheckBod(View.GONE);
                }
                fragment_wtcdwt.adapter.notifyDataSetChanged();
                break;
            case 2:
                for (int i = 0; i < fragment_wsddwt.mdata.size(); i++) {
                    fragment_wsddwt.mdata.get(i).setIsShowCheckBod(View.GONE);
                }
                fragment_wsddwt.adapter.notifyDataSetChanged();
                break;
            case 3:
                for (int i = 0; i < fragment_whddwt.mdata.size(); i++) {
                    fragment_whddwt.mdata.get(i).setIsShowCheckBod(View.GONE);
                }
                fragment_whddwt.adapter.notifyDataSetChanged();
                break;
        }
    }

    private void showCheck() {
        switch (type){
            case 1:
                for (int i = 0; i < fragment_wtcdwt.mdata.size(); i++) {
                    fragment_wtcdwt.mdata.get(i).setIsShowCheckBod(View.VISIBLE);
                }
                try{
                    fragment_wtcdwt.adapter.notifyDataSetChanged();
                }catch (Exception e){

                }

                break;
            case 2:
                for (int i = 0; i < fragment_wsddwt.mdata.size(); i++) {
                    fragment_wsddwt.mdata.get(i).setIsShowCheckBod(View.VISIBLE);
                }
                try{
                    fragment_wsddwt.adapter.notifyDataSetChanged();
                }catch (Exception e){

                }

                break;
            case 3:
                for (int i = 0; i < fragment_whddwt.mdata.size(); i++) {
                    fragment_whddwt.mdata.get(i).setIsShowCheckBod(View.VISIBLE);
                }
                try{
                    fragment_whddwt.adapter.notifyDataSetChanged();
                }catch (Exception e){

                }

                break;
        }
    }

    /**
     * 问答管理请求
     */
    private void requestWDGL() {
        initFragment();//开启问题类型的切换
        try{
            initView();
        }
        catch (Exception e){
            e.toString();
        }
    }

    private void initView() {
        myAnswerRadiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.wdgl_wtcdwx:
                        changeStatu(0);
                        initToolbar_wdgl();
                        if (wdglLl.getVisibility() == View.VISIBLE){
                            showCheck();
                        }
                        break;
                    case R.id.wdgl_wsddwt:
                        changeStatu(1);
                        initToolbar_wdgl();
                        wdglLl.setVisibility(View.GONE);
                        if (wdglLl.getVisibility() == View.VISIBLE){
                            showCheck();
                        }
                        break;
                    case R.id.wdgl_whddwt:
                        changeStatu(2);
                        initToolbar_wdgl();
                        wdglLl.setVisibility(View.GONE);
                        if (wdglLl.getVisibility() == View.VISIBLE){
                            showCheck();
                        }
                        break;
                }
            }
        });
    }


    private void initFragment() {
        fragmentManager = getSupportFragmentManager();
        fragmentArrayList = new ArrayList<Fragment>();
        changeStatu(0);
    }



    /**
     * 根据点击问题的不同类型修改字体颜色
     *
     * @param i
     */
    private void changeStatu(int i) {
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        switch (i) {
            case 0:
                type = 1;
                if(fragment_wtcdwt == null){
                    fragment_wtcdwt = new FragmentWDGL();
                    bundle.putInt("type",type);
                    fragment_wtcdwt.setArguments(bundle);
                    changeFragment(fragment_wtcdwt);
                }else {
                    fragmentTransaction.show(fragment_wtcdwt);
                }

                break;
            case 1:
                type = 2;
                if(fragment_wsddwt == null){
                    fragment_wsddwt = new FragmentWDGL();
                    bundle.putInt("type",type);
                    fragment_wsddwt.setArguments(bundle);
                    changeFragment(fragment_wsddwt);
                }else {
                    fragmentTransaction.show(fragment_wsddwt);
                }
                break;
            case 2:
                type = 3;
                if(fragment_whddwt == null){
                    fragment_whddwt = new FragmentWDGL();
                    bundle.putInt("type",type);
                    fragment_whddwt.setArguments(bundle);
                    changeFragment(fragment_whddwt);
                }else {
                    fragmentTransaction.show(fragment_whddwt);
                }
                break;
        }

        fragmentTransaction.commit();
    }

    private void changeFragment(Fragment fragment) {
        fragmentArrayList.add(fragment);
        fragmentTransaction.add(R.id.wdgl_framLayout_content, fragment);
        fragmentTransaction.show(fragment);
    }

    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for (Fragment fragment : fragmentArrayList) {
            fragmentTransaction.hide(fragment);
        }
    }


    @OnClick({R.id.wdgl_qx, R.id.wdgl_sc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wdgl_qx:

                qxShow();

                //wdglLl.setVisibility(View.GONE);
                break;
            case R.id.wdgl_sc:
                if (FragmentWDGL.refer_id==null||FragmentWDGL.refer_id.size()==0){
                    DialogUtil.showToast(WDGLNEWActivity.this,"请选择删除项");
                    return;
                }
                requestDelWD();//执行删除操作

                wdglLl.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 删除
     */
    private void requestDelWD() {

        new AlertDialog.Builder(WDGLNEWActivity.this)
                .setTitle("提示")
                .setMessage("确认删除？")
                .setNegativeButton("取消",null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        HashMap<String,String> map = new HashMap<>();
                        map.put("code", HttpUtil.getParam());
                        map.put("id",SampleApplicationLike.getUserloginInstance().getId()+"");
                        map.put("refer_id",FragmentWDGL.refer_id.toString());
                        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, RzBean.class, URLAddress.URLBASE + URLAddress.URL_DEL_HD, map, new Response.Listener() {
                            @Override
                            public void onResponse(Object response) {
                                RzBean rZBean = (RzBean) response;
                                if (rZBean.getResult() == 1){
                                    FragmentWDGL.refer_id.clear();
                                    startActivity(new Intent(WDGLNEWActivity.this,WDGLNEWActivity.class));
                                    Toasts.show(WDGLNEWActivity.this, "删除成功", Toast.LENGTH_SHORT);
                                    finish();
                                }else {
                                    Toasts.show(WDGLNEWActivity.this, ""+rZBean.getErr_msg(), Toast.LENGTH_SHORT);
                                }
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                //Toasts.show(WDGLNEWActivity.this, ""+error.toString(), Toast.LENGTH_SHORT);
                                DialogUtil.showToast(WDGLNEWActivity.this,"请选择删除项");
                                ((TextView) toolbar.findViewById(R.id.toolbar_three_text_subtitle)).setText("编辑");
                            }
                        });
                    }
                }).show();

    }

    private void qxShow() {
        switch (type){
            case 1:
                for (int i = 0; i < fragment_wtcdwt.mdata.size(); i++) {
                    fragment_wtcdwt.mdata.get(i).setChoose(true);
                }
                fragment_wtcdwt.adapter.notifyDataSetChanged();
                break;
            case 2:
                for (int i = 0; i < fragment_wsddwt.mdata.size(); i++) {
                    fragment_wsddwt.mdata.get(i).setChoose(true);
                }
                fragment_wsddwt.adapter.notifyDataSetChanged();
                break;
            case 3:
                for (int i = 0; i < fragment_whddwt.mdata.size(); i++) {
                    fragment_whddwt.mdata.get(i).setChoose(true);
                }
                fragment_whddwt.adapter.notifyDataSetChanged();
                break;
        }
    }
}
